/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.saving;

/**
 *
 * @author jay
 */

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.model.SelectItem;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import src.HibernateUtil;
import src.domain.saving.Saving;
import src.services.CRUEDService;
import src.util.Constanta;
import src.util.Utils;

public class SavingModel implements java.io.Serializable {
    
    private CRUEDService service;
    
    public SavingModel() {
        service = new CRUEDService();
    }
    
    public Saving getByAccount(String value) {
        Map<String, String> where = new HashMap<>();
        where.put("sav.accountNo", value);
        where.put("sav.closeDate", null);
        where.put("sav.closedBy", null);
        where.put("sav.approvalUid", "notnull");
//        where.put("sav.savStatus", status.toString());
        //where.put("sav.approvalUid", "notnull");
        //where.put("sav.approvalUid", "notnull");
        return (Saving) service.getObjectSQL("from Saving sav", where);
    }
    
    public Saving getById(Integer id) {
        return (Saving)  service.getById(Saving.class, id);
    }
    
    public List<Saving> findAll(Map<String, String> filters) {
        return (List<Saving>) service.findAll("Saving", filters);
    }
    
    //chan
    //kirim sql lengkap beserta where dan orderby
    public List<Saving> search(String SQLFull) {
        return (List<Saving>) service.findAllSQL(SQLFull, null);
    }
    
    public List<SelectItem> listAll() {
        return service.listAll("Saving", "savaccntId","accountNo");
    }
    
    public boolean saveData(Saving data) {
        return service.saveData(data);
    }
    
    public boolean updateClosureDate(Saving data) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        boolean success;
        
        try {
            
            String hql = "update Saving set closeDate = :closeDate, closedBy = :closedBy where savaccntId = :savaccntId";
            int query = session.createQuery(hql)
            .setDate("closeDate", data.getCloseDate())
            .setString("closedBy", data.getClosedBy())
            .setInteger("savaccntId", data.getSavaccntId())
            .executeUpdate();
 
            tx.commit();
            success = query>0 ? true:false;  
            
        } catch (RuntimeException e) {
            
            if (tx != null && tx.isActive()) {
                try {
                    tx.rollback();
                } catch (HibernateException e1) {}
            }
            success = false;  
        }
        
        return success;
    }
    
    public boolean deleteData(Saving data) {
        return service.deleteData(data);
    }
    
    public Integer authorized(Integer id) {
         return (Integer) service.getObjectSQL(
                 "select approvalUid from Saving where savaccntId = "+id, null);
    }
    
    public boolean isSavingExist(String value, Integer id) {
        BigDecimal count= BigDecimal.ZERO;
        if (value != null && !Constanta.AUTO_NUMBERING.equals(value)) {
            String cond = " where account_no = "+Utils.fmtSQLString(value);
            if (id != null) cond+=" and savaccnt_id <> "+id;
            
            try { count = (BigDecimal) service.getObjectSQLdirect("select count(*) from saving "+cond);
            } catch (RuntimeException e) {count = BigDecimal.ZERO;}
        }
        return (count.compareTo(BigDecimal.ZERO)==0 ? false : true);
    }
    
}
